package com.yulian.util;

import java.util.Collection;
import java.util.Map;

/**
 * @author:  
 * date: 2023/7/30 18:55
 * @since: JDK 1.8
 *
 * @description: 数据工具类
 */
public final class DataUtils {
    private DataUtils() {
    }

    /**
     * 判断对象是否Empty(null或元素为0)<br>
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     *
     * @param pObj 待检查对象
     * @return boolean 返回的布尔值
     */
    public static final boolean isEmpty(Object pObj) {
        if (pObj == null || "".equals(pObj)) {
            return true;
        }
        if (pObj instanceof String) {
            if (((String) pObj).trim().length() == 0) {
                return true;
            }
        } else if (pObj instanceof Long) {
            if (String.valueOf(pObj).trim().length() == 0) {
                return true;
            }
        } else if (pObj instanceof Integer) {
            if (String.valueOf(pObj).trim().length() == 0) {
                return true;
            }
        }else if (pObj instanceof Collection<?>) {
            if (((Collection<?>) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Map<?, ?>) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断对象是否为NotEmpty(!null或有元素)<br>
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     *
     * @param pObj 待检查对象
     * @return boolean 返回的布尔值
     */
    public static final boolean isNotEmpty(Object pObj) {
        if (pObj == null || "".equals(pObj)) {
            return false;
        }
        if (pObj instanceof String) {
            if (((String) pObj).trim().length() == 0) {
                return false;
            }
        }else if (pObj instanceof Long) {
            if (String.valueOf(pObj).trim().length() == 0) {
                return false;
            }
        } else if (pObj instanceof Integer) {
            if (String.valueOf(pObj).trim().length() == 0) {
                return false;
            }
        } else if (pObj instanceof Collection<?>) {
            if (((Collection<?>) pObj).size() == 0) {
                return false;
            }
        } else if (pObj instanceof Map<?, ?>) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return false;
            }
        }
        return true;
    }

    public static final boolean isEmpty(Object... objects) {
        for (Object object : objects) {
            if (Boolean.FALSE.equals(isNotEmpty(object))) {
                return true;
            }
        }
        return false;
    }
}

